<template>
	<div>
		<Button class="addbutton" icon="plus-round" type="dashed" @click="addImprove">新增改善期刊</Button>
		<v-table ref="vTable" :tableData="tableData" :tableColumns="tableColumns"></v-table>
		
		<Modal v-model="isImprove" class-name="eidthform" :width="400" :mask-closable='false'>
			<p slot="header">新增改善期刊</p>
			<Form ref="improveForm" :model="improveData" :label-width="70">
				<FormItem label="改善期刊:">
	                <Input type="text" v-model="improveData.ImprovePeriodicalName" placeholder=""></Input>
	           </FormItem>
			</Form>
			<div slot="footer" class="button-box">
	            <Button type="primary" :loading="improveKeep"  @click="keepImprove('improveForm')">保存</Button>
	            <Button type="ghost" @click="improveCancel('improveForm', 'isImprove')">取消</Button>
	        </div>
		</Modal>
	</div>
</template>

<script>
	import vTable from '@/components/common/Table.vue'
	export default {
		components: {
			vTable
		},
		data(){
			return {
				isImprove: false,
				improveKeep: false,
				customer_id: '',
				ImprovePeriodicalId: '',
				improveData:{
					customer_id: '',
					ImprovePeriodicalName: ''
				},
				tableData: [],
				tableColumns: [
					{ type: "index", align: "center", width: 60 },
					{ title: "改善方案期刊", key: "improveName", align: "center" },
					{ title: "时间", key: "createTime", align: "center" },
					{
                        title: '操作',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: { type: 'primary', size: 'small' },
                                    style: { marginRight: '5px' },
                                    on: {
                                        click: () => {
											this.$router.push({
												path: '/improve/form/' + params.row.id,
												query: {customer_id: this.customer_id}
											}) 
                                        }
                                    }
                                }, '查看')
                            ]);
                        }
                    },
                    {
                        title: '推送',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: { type: 'primary', size: 'small' },
                                    style: { marginRight: '5px' },
                                    on: {
                                        click: () => {
                                        	var push_improve_periodical = '/improve/push_improve_periodical/'+params.row.id+'?ImprovePeriodicalId=' +  this.ImprovePeriodicalId;
                                        	this.$http.post(push_improve_periodical)
                                        	.then(({data})=>{
                                        		console.log(data)
                                        		if(data.status=="success"){
                                        			this.$Message.info('推送成功!');
                                        		}
                                        	})
                                        	.catch((err)=>{
                                        		console.log(err)
                                        	})
                                        }
                                    }
                                }, '推送')
                            ]);
                        }
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: { type: 'error', size: 'small' },
                                    style: { marginRight: '5px' },
                                    on: {
                                        click: () => {
                                        	console.log(params.row.improveName)
                                        	this.$Modal.confirm({
                                        		title: "删除确认",
                                        		content:`<p>改善方案期刊:  ${params.row.improveName}</p>`,
                                        		okText: "确认",
						                        cancelText: "取消",
						                        loading: true,
						                        onOk: () => { 
						                        	//   删除改善周刊
						                        	this.$http.post('/improve/remove_improve_periodical',{ImprovePeriodicalId: params.row.id})
						                        	.then((res)=>{
						                        		console.log(res)
						                        		this.$Modal.remove();
						                        		this.tableData.splice(params.index, 1);
						                        		this.$Message.success("删除成功!");
						                        		this.getData();
						                        	})
						                        	.catch((err)=>{
						                        		console.log(err);
						                        		this.$Notice.error({ title: "删除失败！" });
						                        	})
						                        }
                                        	})
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
				]
			}
		},
		created(){
			this.customer_id = this.$route.params.id;
			this.getData();
		},
		methods:{
			//  显示改善方案期刊 模态框  POST {customer_id}
			addImprove(){
				this.isImprove = true;
			},
			//  保存改善方案期刊
			keepImprove(){
				this.improveData.customer_id = this.$route.params.id;
				this.$http.post('/improve/add_improve_periodical/' + this.customer_id,this.improveData)
				.then((res)=>{
//					console.log(res);
					this.isImprove = false;
					this.getData();
				})
				.catch((err)=>{
					console.log(err);
				})
			},
			//  取消改善方案期刊 模态框
			improveCancel(refName, showName){
				this.$refs[refName].resetFields();
				this[showName] = false;
			},
			//  获取改善期刊列表
			getData(){  
				this.$http.get('/improve/improve_periodical_list/' + this.customer_id)
				.then(({data})=>{
					console.log(data)
					this.tableData = data.data.map(e=>{
						this.ImprovePeriodicalId = e.id;
						//  时间戳转时间
						var date = new Date(e.createTime);
						let Y = date.getFullYear() + '-';
						let M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
						let D = date.getDate();
						D = D < 10 ? '0' + date.getDate() : D;
						e.createTime = Y + M + D;
						return e;
					})
				})
				.catch((err)=>{
					console.log(err);
				})
			}
		}
	}
</script>

<style>
</style>